package com.skyworthclub.competition.controller.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * @author li
 * @version 1.0
 * @since 18-10-17 下午12:51
 * 空指针异常处理器
 **/
@RestControllerAdvice
public class HttpMessageNotReadableExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(HttpMessageNotReadableExceptionHandler.class);

    /**
     * 处理客户端参数传递错误
     *
     * @param ex 异常对象
     * @return 状态信息
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseEntity<Map<String, Object>> handleHttpMessageNotReadable(Throwable ex) {
        logger.error(ex.getMessage(), ex);
        Map<String, Object> map = new HashMap<>(4);
        map.put("status", HttpStatus.BAD_REQUEST);
        map.put("message", "请求参数错误！");
        return new ResponseEntity<>(map, HttpStatus.BAD_REQUEST);
    }

}
